package com.guoheng.safe.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoheng.safe.common.base.Result;
import com.guoheng.safe.common.constant.CommonConstants;
import com.guoheng.safe.common.enums.ErrorCodeEnum;
import com.guoheng.safe.sys.dto.basicinfo.PromiseDatePageDTO;
import com.guoheng.safe.sys.dto.basicinfo.PromiseDataAddDTO;
import com.guoheng.safe.sys.model.basicinfo.PromiseData;
import com.guoheng.safe.sys.service.PromiseDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.guoheng.safe.sys.mapper.basicinfo.PromiseDataMapper;

import java.util.Date;
import java.util.List;

/**
 * 类功能描述:　PromiseDataServiceImpl
 *
 * @author Eternal
 * @date 2019-50-22 14:50
 */
@Service
public class PromiseDataServiceImpl implements PromiseDataService {

    @Resource
    private PromiseDataMapper promiseDataMapper;


    @Override
    public Result addPromiseData(PromiseDataAddDTO promiseDataAddDTO) {
        PromiseData promiseData = new PromiseData();
        BeanUtils.copyProperties(promiseDataAddDTO,promiseData);
        int promiseNum = promiseDataMapper.countPromiseNumByDate(promiseDataAddDTO.getPromiseTime()) +1;
        promiseData.setTodayPromiseNum("第" + promiseNum + "次");
        promiseData.setCreateTime(new Date());
        int result = promiseDataMapper.insertSelective(promiseData);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_CREATE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result getPromiseDataPage(PromiseDatePageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum() , pageDTO.getPageSize());
        List<PromiseData> promiseData = promiseDataMapper.selectAll();
        PageInfo<PromiseData> pageInfo = new PageInfo<>(promiseData);
        return Result.success(pageInfo);
    }
}


